import {
	defineStore
} from 'pinia'
import {
	getConfigAPI,
	register,
	smsCode,
	loginAPI,
	forgetPwdAPI,
	getUserInfoAPI,
	emitUserInfoAPI,
	getUserOutSideAPI,
	emitUserOutSideAPI,
	emitOutSideSwitchAPI,
	feedBackAPI,
	userPageAPI,
	followOrCloseAPI,
	followListAPI,
	fansListAPI,
	taskListAPI,
	moneyAmountAPI,
	moneyRecordsListAPI,
	rechargeTemplateAPI,
	userAccountBindAPI,
	editAccountBindAPI,
	applyWithdrawAPI,
	userAuthStatusAPI,
	userAuthInfoAPI,
	editUserAuthInfoAPI,
	userReceiveAPI,
	userRechargeAPI,
	shareQrCodeAPI,
	mapDataAPI,
	getUserActivityListAPI,
	getUserTaskListAPI,
	getUserApplyListAPI,
	getFavoriteActivityListAPI,
	getFavoriteTasksListAPI,
	logoutAPI,
	bindOpenidAPI,
	visitShareCount,
	getUserShareRecords
} from '@/api/user.js'
import {
	isPhone
} from '@/utils/utils.js'
export const useUserStore = defineStore('user', {
	state: () => {
		return {
			tabIndex: 0,
			time: 60,
			codeTxt: '点击获取',
			code: '',
			token: '',
			userInfo: {},
			outSideInfo: {},
			userPageInfo: {},
			taskList: [],
			totalMoney: 0,
			rechargeTemplateList: [], //充值模版列表
			accountBindInfo: {}, //提现账号绑定信息
			authStatusInfo: {}, //认证状态信息
			selfAuthInfo: {}, //个人认证信息
			companyAuthInfo: {}, //公司认证信息
		}
	},

	getters: {},
	actions: {
		// 注册、忘记密码
		async registerOrForgetPwd(data, type) {
			if (type == 1) {
				// 1:注册 2：忘记密码
				const result = await register(data)
					return result
			} else {
				const res = await forgetPwdAPI(data)
				if (res.code == 200) {
					uni.$uv.toast('修改密码成功')
					uni.navigateBack()
				} else {
					uni.$uv.toast(res.message)
				}
			}
		},
		// 分享邀约
		async visitShareCount(data) {
			const res = await visitShareCount(data)
			return res
		},
		// 获取验证码
		async getCode(data) {
			const res = await smsCode(data)
			return res
		},
		// 登录
		async login(data) {
			const res = await loginAPI(data)
			return res
		},
		// 获取用户信息
		async getUserInfo() {
			const res = await getUserInfoAPI()
			this.userInfo = res.data
			return res
		},
		// 编辑用户信息
		async emitUserInfo(data) {
			if (data.avatar == '') {
				uni.$uv.toast('请上传头像')
				return
			}
			if (data.username == '') {

				uni.$uv.toast('请填写昵称')
				return
			}
			if (data.sex == '') {
				uni.$uv.toast('请选择性别')
				return
			}
			if (data.age == '') {
				uni.$uv.toast('请输入年龄段')
				return
			}
			if (data.province_id == 0) {
				uni.$uv.toast('请选择您所在的位置')
				return
			}
			const res = await emitUserInfoAPI(data)
			return res
		},
		// 获取用户站外联系方式
		async getOutSideInfo() {
			let res = await getUserOutSideAPI()
			// this.outSideInfo = res.data
			return res
		},
		// 编辑用户外站联系方式
		async emitOutSideInfo(data) {
			let res = await emitUserOutSideAPI(data)
			return res
		},
		// 修改用户站外联系方式展示状态
		async changeStatus(data) {
			let res = await emitOutSideSwitchAPI(data)
		},
		// 提交用户反馈
		async submitFeedBack(data) {
			if (uni.$uv.trim(data.content, 'all') == '') {
				uni.$uv.toast('请输入您的宝贵意见')
				return
			}
			let res = await feedBackAPI(data)
			if (res.code == 200) {
				uni.$uv.toast('提交成功')
				setTimeout(() => {
					uni.navigateBack()
				}, 500)
			}
		},
		// 获取用户主页
		async getUserPage(data) {
			let res = await userPageAPI(data)
			this.userPageInfo = res.data
		},
		// 用户关注/取关
		async followOrClose(data) {
			let res = await followOrCloseAPI(data)
			return res
		},
		// 关注列表
		async getFollowList(data) {
			let res = await followListAPI(data)
			return res
		},
		// 粉丝列表
		async getFansList(data) {
			let res = await fansListAPI(data)
			return res
		},
		// 邀请记录
		async getUserShareRecords(data) {
			let res = await getUserShareRecords(data)
			return res
		},
		// 获取任务奖励列表
		async getTaskList() {
			let res = await taskListAPI()
			this.taskList = res.data
			this.taskList.push({
				title:'创新建议',
				image:'/uploads/20240815/b6e341ae938cca6e26cefd41e3042985.png',
				prize_money:'10000',
				task_status:'0'
			})
		},
		// 获取星光余额
		async getMoneyAmount() {
			let res = await moneyAmountAPI()
			this.totalMoney = res.data.now_money
			return this.totalMoney
		},
		// 收支明细列表
		async getMoneyRecordsList(data) {
			let res = await moneyRecordsListAPI(data)
			return res
		},
		// 获取充值选项模版列表
		async getRechargeTemplateList() {
			let res = await rechargeTemplateAPI()
			this.rechargeTemplateList = res.data
		},
		// 获取绑定账户
		async getAccountBindInfo() {
			let res = await userAccountBindAPI()
			this.accountBindInfo = res.data
		},
		// 编辑用户绑定账户
		async editAccountBindInfo(data) {
			let res = await editAccountBindAPI(data)
			return res
		},
		// 获取公用数据
		async getConFig(data) {
			let res = await getConfigAPI(data)
			return res
		},
		// 提现
		async applyWithdraw(data) {
			let res = await applyWithdrawAPI(data)
			return res
		},
		// 获取用户认证状态
		async getUserAuthStatus() {
			let res = await userAuthStatusAPI()
			this.authStatusInfo = res.data
			return res
		},
		// 获取用户认证信息
		async getUserAuthInfo(data) {
			let res = await userAuthInfoAPI(data)
			if (data.type == 'individual') {
				this.selfAuthInfo = res.data
			} else {
				this.companyAuthInfo = res.data
			}

		},
		// 编辑用户认证信息
		async editUserAuth(data) {
			let res = await editUserAuthInfoAPI(data)
			return res
		},
		// 用户打卡
		async userReceive(data) {
			let res = await userReceiveAPI(data)
			return res
		},
		// 用户充值
		async userRecharge(data) {
			let res = await userRechargeAPI(data)
			return res
		},
		// 海报二维码
		async getQrcode() {
			let res = await shareQrCodeAPI()
			return res
		},
		// 省市列表
		async getMapDataList() {
			let res = await mapDataAPI()
			return res
		},
		// 获取用户发布的活动列表
		async getUserActivityList(data) {
			let res = await getUserActivityListAPI(data)
			return res
		},
		// 获取用户发布找伴任务列表
		async getUserTaskList(data) {
			let res = await getUserTaskListAPI(data)
			return res
		},
		// 获取用户报名参加活动
		async getUserApplyList(data) {
			let res = await getUserApplyListAPI(data)
			return res
		},
		// 获取用户收藏活动列表
		async getFavoriteActivityList(data) {
			let res = await getFavoriteActivityListAPI(data)
			return res
		},
		// 获取用户收藏找伴任务列表
		async getFavoriteTasksList(data) {
			let res = await getFavoriteTasksListAPI(data)
			return res
		},
		// 退出登录
		async logout() {
			let res = await logoutAPI()
			if (res.code == 200) {
				uni.$uv.toast('退出登录成功')
				uni.removeStorageSync('token');
				uni.removeStorageSync('expireTime')
				uni.reLaunch({
					url: '/pages/login/index'
				})
			}
		},
		// 绑定用户openid
		async bindOpenid(data) {
			let res = await bindOpenidAPI(data)
			return res
		},
	},
	// 开启数据持久化
	persist: {
		enabled: true,
		strategies: [{
			storage: localStorage,
			paths: ['userInfo', 'userPageInfo', 'authStatusInfo',
				'selfAuthInfo', 'companyAuthInfo'
			]
		}]
	},
})